package com.samsung.dementia.protocol;

import com.samsung.dementia.database.MySQLManager;
import com.samsung.dementia.database.SqlResult;

import net.sf.json.JSONObject;

public class CommandProcessor {

	public static JSONObject insertDailyDQData(JSONObject jsonObject) {
		
		String id = jsonObject.getString("id");
		
		String data = jsonObject.getString("data");
		
		String timestamp = jsonObject.getString("timestamp");
		
		String year = jsonObject.getString("year");
		String month = jsonObject.getString("month");
		String dayOfMonth = jsonObject.getString("dayOfMonth");
		String hour = jsonObject.getString("hour");
		String minute = jsonObject.getString("minute");
		String second = jsonObject.getString("second");
				
		JSONObject ret = new JSONObject();

		if (MySQLManager.getInstance().connect()) {
			
			SqlResult sqlResult = MySQLManager.getInstance().insertDQData(id, data, timestamp, year, month, dayOfMonth, hour, minute, second);
			
			ret.put("code", sqlResult.getCode());
			ret.put("message", sqlResult.getMessage());
			
		} else {
			System.out.println("MySql Msg: MySQL Connection is failed");
			ret.put("message", "MySQL connection is failed");
		}
		
		ret.put("command", jsonObject.getString("command"));

		return ret;
	}
	
	public static JSONObject insertDailyLocationData(JSONObject jsonObject)
	{		
		String id = jsonObject.getString("id");
		String data = jsonObject.getString("data");
		String timestamp = jsonObject.getString("timestamp");
		String year = jsonObject.getString("year");
		String month = jsonObject.getString("month");
		String dayOfMonth = jsonObject.getString("dayOfMonth");
		String hour = jsonObject.getString("hour");
		String minute = jsonObject.getString("minute");
		String second = jsonObject.getString("second");
		
		JSONObject ret = new JSONObject();
		
		if(MySQLManager.getInstance().connect())
		{
			SqlResult sqlResult = MySQLManager.getInstance().insertLocationData(id, data, timestamp, year, month, dayOfMonth, hour, minute, second);
			
			ret.put("code", sqlResult.getCode());
			ret.put("message", sqlResult.getMessage());
			
		} else {
			System.out.println("MySql Msg: MySQL Connection is failed");
			ret.put("message", "MySQL connection is failed");
		}
		
		ret.put("command", jsonObject.getString("command"));
		
		return ret;
	}
	
	public static JSONObject insertHumanMovmentRecognitionData(JSONObject jsonObject)
	{
		String id = jsonObject.getString("id");
		String data = jsonObject.getString("data");
		String timestamp = jsonObject.getString("timestamp");
		String year = jsonObject.getString("year");
		String month = jsonObject.getString("month");
		String dayOfMonth = jsonObject.getString("dayOfMonth");
		String hour = jsonObject.getString("hour");
		String minute = jsonObject.getString("minute");
		String second = jsonObject.getString("second");
		
		JSONObject ret = new JSONObject();
		
		if(MySQLManager.getInstance().connect())
		{
			SqlResult sqlResult = MySQLManager.getInstance().insertHMRData(id, data, timestamp, year, month, dayOfMonth, hour, minute, second);
			
			ret.put("code", sqlResult.getCode());
			ret.put("message", sqlResult.getMessage());
			
		} else {
			System.out.println("MySql Msg: MySQL Connection is failed");
			ret.put("message", "MySQL connection is failed");
		}
		
		ret.put("command", jsonObject.getString("command"));
		
		return ret;
	}
	
	public static JSONObject insertAppInfo(JSONObject jsonObject)
	{
		String id = jsonObject.getString("id");
		String data = jsonObject.getString("data");
		String timestamp = jsonObject.getString("timestamp");
		String year = jsonObject.getString("year");
		String month = jsonObject.getString("month");
		String dayOfMonth = jsonObject.getString("dayOfMonth");
		String hour = jsonObject.getString("hour");
		String minute = jsonObject.getString("minute");
		String second = jsonObject.getString("second");
		
		JSONObject ret = new JSONObject();
		
		if(MySQLManager.getInstance().connect())
		{
			SqlResult sqlResult = MySQLManager.getInstance().insertAppInfo(id, data, timestamp, year, month, dayOfMonth, hour, minute, second);
			
			ret.put("code", sqlResult.getCode());
			ret.put("message", sqlResult.getMessage());
			
		} else {
			System.out.println("MySql Msg: MySQL Connection is failed");
			ret.put("message", "MySQL connection is failed");
		}
		
		ret.put("command", jsonObject.getString("command"));
		
		return ret;
	}
	
	public static JSONObject insertSocialData(JSONObject jsonObject)
	{
		String id = jsonObject.getString("id");
		String data = jsonObject.getString("data");
		String timestamp = jsonObject.getString("timestamp");
		String year = jsonObject.getString("year");
		String month = jsonObject.getString("month");
		String dayOfMonth = jsonObject.getString("dayOfMonth");
		String hour = jsonObject.getString("hour");
		String minute = jsonObject.getString("minute");
		String second = jsonObject.getString("second");
		
		JSONObject ret = new JSONObject();
		
		if(MySQLManager.getInstance().connect())
		{
			SqlResult sqlResult = MySQLManager.getInstance().insertSocialData(id, data, timestamp, year, month, dayOfMonth, hour, minute, second);
			
			ret.put("code", sqlResult.getCode());
			ret.put("message", sqlResult.getMessage());
			
		} else {
			System.out.println("MySql Msg: MySQL Connection is failed");
			ret.put("message", "MySQL connection is failed");
		}
		
		ret.put("command", jsonObject.getString("command"));
		
		return ret;
	}

	public static JSONObject compareLoginInfoAndupdateTimestamp(JSONObject jsonObject) {
		
		String id = jsonObject.getString("id");
		String pwd = jsonObject.getString("pwd");
		String timestamp = jsonObject.getString("timestamp");
		String modelName = jsonObject.getString("model_name");
		String androidVersion = jsonObject.getString("android_version");
		
		JSONObject ret = new JSONObject();
		
		int code;
		String message;
		String query;

		if (MySQLManager.getInstance().connect()) {

			SqlResult sqlResult;
			
			boolean isSame = MySQLManager.getInstance().compareLoginInfo(id, pwd);
			if(isSame) {
				sqlResult = MySQLManager.getInstance().updataTimestampForUserAccount(id, pwd, timestamp, modelName, androidVersion);
				
				code = sqlResult.getCode();
				message = sqlResult.getMessage();
				query = sqlResult.getQuery();
			} else {
				code = -1;
				message = "User account did not exist";
				query = "";
			}
		} else {
			System.out.println("MySql Msg: Connection is failed");
			
			code = -1;
			message = "mysql connection is failed";
			query = "";
		}
		
		ret.put("code", code);
		ret.put("message", message);
		ret.put("query", query);
		ret.put("command", jsonObject.getString("command"));

		return ret;
	}
	
	public static JSONObject checkServerAlive(JSONObject jsonObject) {
		JSONObject ret = new JSONObject();

		String id = jsonObject.getString("id");
		String accessTime = jsonObject.getString("access_time");

		if (MySQLManager.getInstance().connect()) {

			SqlResult sqlResult = MySQLManager.getInstance().updateUserAccessTime(id, accessTime);
			if (sqlResult.getCode() >= 0) {
				ret.put("message", "user time check is ok");
				ret.put("code", "0");
			} else {
				ret.put("message", "user time check failed");
				ret.put("code", "-1");
			}
		}
		
		ret.put("command", jsonObject.getString("command"));

		return ret;
	}
}
